package com.oro.converters;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import org.springframework.web.jsf.FacesContextUtils;

import com.oro.domain.Banca;
import com.oro.service.StudentService;

public class BancaStringConverter implements Converter {

	private List<Banca> banche;

	@Override
	public Object getAsObject(FacesContext context, UIComponent component,
			String value) throws ConverterException {

		StudentService studentService = (StudentService) FacesContextUtils
				.getWebApplicationContext(context).getBean("studentService");

		banche = studentService.getBanks();

		for (Banca b : banche) {
			if (b.toString().equals(value))
				return b;
		}

		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object value) throws ConverterException {

		if (value != null)
			return ((Banca) value).toString();
		else
			return null;

	}

}
